package com.rk.adapter;

import java.util.List;

import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.carboy.launch.R;
import com.rk.entity.AppInfo;

public class AppAdapter extends BaseAdapter {
    private static final String TAG = "JK_AppAdapter";
    private Context mContext;
    private List<AppInfo> mAppInfos;
    private int mAvailableWidth, mAvailableHeight;

    public AppAdapter(Context context, List<AppInfo> appInfos, int availableWidth, int availableHeight) {
        this.mContext = context;
        this.mAppInfos = appInfos;
        this.mAvailableWidth = availableWidth;
        this.mAvailableHeight = availableHeight;
    }

    @Override
    public int getCount() {  
        return mAppInfos.size();
    }

    @Override
    public Object getItem(int arg0) {
        return mAppInfos.get(arg0);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if(convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_app, null);
            convertView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    Intent intent = new Intent(Intent.ACTION_MAIN)
                            .addCategory(Intent.CATEGORY_LAUNCHER)
                            .setFlags(
                                    Intent.FLAG_ACTIVITY_NEW_TASK
                                        | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                    ComponentName com = new ComponentName(mAppInfos.get(position).getPackageName(), mAppInfos.get(position).getActivityName());
                    intent.setComponent(com);
                    try {
                        mContext.startActivity(intent);
                    } catch(ActivityNotFoundException e) {
                        Log.e(TAG, "occur activity not found exception");
                    }
                }
            });
            ((LinearLayout)convertView).setLayoutParams(new LinearLayout.LayoutParams(mAvailableWidth, mAvailableHeight));
            viewHolder.appIcon = (ImageView) convertView.findViewById(R.id.item_app_icon);
            int length = 80;
            if(mAvailableWidth > mAvailableHeight) {
                length = mAvailableHeight / 2;
            } else {
                length = mAvailableWidth / 2;
            }
            viewHolder.appIcon.setLayoutParams(new LinearLayout.LayoutParams(length, length));
            viewHolder.appName = (TextView) convertView.findViewById(R.id.item_app_name);
            viewHolder.appName.setTextColor(mContext.getResources().getColor(R.color.white));
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.appIcon.setImageDrawable(mAppInfos.get(position).getIcon());
        viewHolder.appName.setText(mAppInfos.get(position).getAppName());
        return convertView;
    }

    class ViewHolder {
        ImageView appIcon;
        TextView appName;
    }
}
